package psn.zjk.common.jdk.juc;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @USER white
 * @DATETIME 2021/1/8 12:59
 */
public class JucDemo {
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20, 10000, TimeUnit.SECONDS, new SynchronousQueue<>());

    /** 我等他们做完，再继续 */
    public void countDownLatch() throws InterruptedException {
        int n = 10;
        CountDownLatch countDownLatch = new CountDownLatch(n);

        long l = System.currentTimeMillis();
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            int finalI = i;
            threadPoolExecutor.execute(() -> {
                //other.doSth(); other不阻塞
                try {
                    Thread.sleep(random.nextInt(10)*1000);
                } catch (InterruptedException e) {
                }
                countDownLatch.countDown();
                System.out.println(System.currentTimeMillis()+"==="+finalI + " 我好了");
            });
        }
        //只有自己阻塞
        countDownLatch.await();
        System.out.println(System.currentTimeMillis()+"==="+"那我也好了");
    }

    /** 等所有人达到再一起行动 */
    public void cyclicBarrier() throws BrokenBarrierException, InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
        int n = 10;
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            int finalI = i;
            threadPoolExecutor.execute(() -> {
                try {
                    Thread.sleep(random.nextInt(10)*1000);
                    //parter.doSth();所有人都会阻塞
                    cyclicBarrier.await();
                    System.out.println(System.currentTimeMillis()+"==="+finalI + " 我好了");
                    //ok,go!
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        cyclicBarrier.await();
        System.out.println(System.currentTimeMillis()+"==="+"我也好了");
    }

    /** 等所有人达到再一起行动 */
    public void semaphore() throws BrokenBarrierException, InterruptedException {
        Semaphore semaphore = new Semaphore(10);
        int n = 10;

        long l = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            int finalI = i;
            threadPoolExecutor.execute(() -> {
                int i1 = new Random().nextInt(n);
                try {
                    System.out.println("申请"+i1);
                    if (semaphore.tryAcquire(i1)) {
                        //parter.doSth();
                        System.out.println(System.currentTimeMillis()+"==="+finalI + " 我申请["+i1+"]通过了");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }

    public static void main(String[] args) throws Exception {
        JucDemo jucDemo = new JucDemo();
        System.out.println("countDownLatch");
        jucDemo.countDownLatch();
        System.out.println("cyclicBarrier");
        jucDemo.cyclicBarrier();
        Thread.sleep(1000);
        System.out.println("semaphore");
        jucDemo.semaphore();
    }


}
